@@ -70,9 +70,9 @@ module Agents |
||
| 70 | 70 |
incoming_events.each do |event| |
| 71 | 71 |
outgoing = interpolated(event.payload)['payload'].presence || {}
|
| 72 | 72 |
if interpolated['no_merge'].to_s == 'true' |
| 73 |
- handle outgoing |
|
| 73 |
+ handle outgoing, event.payload |
|
| 74 | 74 |
else |
| 75 |
- handle outgoing.merge(event.payload) |
|
| 75 |
+ handle outgoing.merge(event.payload), event.payload |
|
| 76 | 76 |
end |
| 77 | 77 |
end |
| 78 | 78 |
end |
@@ -81,35 +81,35 @@ module Agents |
||
| 81 | 81 |
handle interpolated['payload'].presence || {}
|
| 82 | 82 |
end |
| 83 | 83 |
|
| 84 |
- def generate_uri(params = nil) |
|
| 85 |
- uri = URI interpolated[:post_url] |
|
| 84 |
+ def generate_uri(params = nil, payload = {})
|
|
| 85 |
+ uri = URI interpolated(payload)[:post_url] |
|
| 86 | 86 |
uri.query = URI.encode_www_form(Hash[URI.decode_www_form(uri.query || '')].merge(params)) if params |
| 87 | 87 |
uri |
| 88 | 88 |
end |
| 89 | 89 |
|
| 90 | 90 |
private |
| 91 | 91 |
|
| 92 |
- def handle(data) |
|
| 92 |
+ def handle(data, payload = {})
|
|
| 93 | 93 |
if method == 'post' |
| 94 |
- post_data(data, Net::HTTP::Post) |
|
| 94 |
+ post_data(data, payload, Net::HTTP::Post) |
|
| 95 | 95 |
elsif method == 'put' |
| 96 |
- post_data(data, Net::HTTP::Put) |
|
| 96 |
+ post_data(data, payload, Net::HTTP::Put) |
|
| 97 | 97 |
elsif method == 'delete' |
| 98 |
- post_data(data, Net::HTTP::Delete) |
|
| 98 |
+ post_data(data, payload, Net::HTTP::Delete) |
|
| 99 | 99 |
elsif method == 'patch' |
| 100 |
- post_data(data, Net::HTTP::Patch) |
|
| 100 |
+ post_data(data, payload, Net::HTTP::Patch) |
|
| 101 | 101 |
elsif method == 'get' |
| 102 |
- get_data(data) |
|
| 102 |
+ get_data(data, payload) |
|
| 103 | 103 |
else |
| 104 | 104 |
error "Invalid method '#{method}'"
|
| 105 | 105 |
end |
| 106 | 106 |
end |
| 107 | 107 |
|
| 108 |
- def post_data(data, request_type = Net::HTTP::Post) |
|
| 109 |
- uri = generate_uri |
|
| 108 |
+ def post_data(data, payload, request_type = Net::HTTP::Post) |
|
| 109 |
+ uri = generate_uri(nil, payload) |
|
| 110 | 110 |
req = request_type.new(uri.request_uri, headers) |
| 111 | 111 |
|
| 112 |
- if interpolated['content_type'] == 'json' |
|
| 112 |
+ if interpolated(payload)['content_type'] == 'json' |
|
| 113 | 113 |
req.set_content_type('application/json', 'charset' => 'utf-8')
|
| 114 | 114 |
req.body = data.to_json |
| 115 | 115 |
else |
@@ -119,8 +119,8 @@ module Agents |
||
| 119 | 119 |
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) }
|
| 120 | 120 |
end |
| 121 | 121 |
|
| 122 |
- def get_data(data) |
|
| 123 |
- uri = generate_uri(data) |
|
| 122 |
+ def get_data(data, payload) |
|
| 123 |
+ uri = generate_uri(data, payload) |
|
| 124 | 124 |
req = Net::HTTP::Get.new(uri.request_uri, headers) |
| 125 | 125 |
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) }
|
| 126 | 126 |
end |
@@ -28,8 +28,8 @@ describe Agents::PostAgent do |
||
| 28 | 28 |
@requests = 0 |
| 29 | 29 |
@sent_requests = { Net::HTTP::Get => [], Net::HTTP::Post => [], Net::HTTP::Put => [], Net::HTTP::Delete => [], Net::HTTP::Patch => [] }
|
| 30 | 30 |
|
| 31 |
- stub.any_instance_of(Agents::PostAgent).post_data { |data, type| @requests += 1; @sent_requests[type] << data }
|
|
| 32 |
- stub.any_instance_of(Agents::PostAgent).get_data { |data| @requests += 1; @sent_requests[Net::HTTP::Get] << data }
|
|
| 31 |
+ stub.any_instance_of(Agents::PostAgent).post_data { |data, payload, type| @requests += 1; @sent_requests[type] << data }
|
|
| 32 |
+ stub.any_instance_of(Agents::PostAgent).get_data { |data, payload| @requests += 1; @sent_requests[Net::HTTP::Get] << data }
|
|
| 33 | 33 |
end |
| 34 | 34 |
|
| 35 | 35 |
describe "making requests" do |
@@ -225,7 +225,17 @@ describe Agents::PostAgent do |
||
| 225 | 225 |
it "just returns the post_uri when no params are given" do |
| 226 | 226 |
@checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value" |
| 227 | 227 |
uri = @checker.generate_uri |
| 228 |
+ uri.host.should == 'example.com' |
|
| 229 |
+ uri.scheme.should == 'http' |
|
| 228 | 230 |
uri.request_uri.should == "/a/path?existing_param=existing_value" |
| 229 | 231 |
end |
| 232 |
+ |
|
| 233 |
+ it "interpolates when receiving a payload" do |
|
| 234 |
+ @checker.options['post_url'] = "https://{{ domain }}/{{ variable }}?existing_param=existing_value"
|
|
| 235 |
+ uri = @checker.generate_uri({ "some_param" => "some_value", "another_param" => "another_value" }, { 'domain' => 'google.com', 'variable' => 'a_variable' })
|
|
| 236 |
+ uri.request_uri.should == "/a_variable?existing_param=existing_value&some_param=some_value&another_param=another_value" |
|
| 237 |
+ uri.host.should == 'google.com' |
|
| 238 |
+ uri.scheme.should == 'https' |
|
| 239 |
+ end |
|
| 230 | 240 |
end |
| 231 | 241 |
end |